package foodmaterial.action.valid;

import foodmaterial.utils.Constants;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PaginationNoValidator implements ConstraintValidator<PaginationNoValid, Integer> {

    private PaginationNoValid constraintAnnotation ;

    @Override
    public void initialize(PaginationNoValid constraintAnnotation) {
        this.constraintAnnotation = constraintAnnotation ;
    }

    @Override
    public boolean isValid(Integer pageNo, ConstraintValidatorContext constraintValidatorContext) {
        if (constraintAnnotation.ignoreEmpty() && pageNo == null) return true ;
        if (pageNo == null) return false ;
        if (pageNo.intValue() > Constants.PAGE_SIZE.intValue() || pageNo.intValue() <= 0) return false ;
        return true;
    }
}
